package com.letv.whatslive.mongo.dao;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.GlobalSwitch;
import com.letv.whatslive.model.PictureCompressHistory;
import com.letv.whatslive.model.convert.GlobalSwitchConvert;
import com.letv.whatslive.model.convert.PictureCompressHistoryConvert;
import com.letv.whatslive.mongo.BaseDAO;
import com.mongodb.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by haojiayao on 16-3-16.
 */
@Repository
public class PictureCompressHistoryDAO extends BaseDAO {

    protected String collectionName = "pictureCompressHistory";



    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }



    public List<PictureCompressHistory> getPictureCompressHistoryListByParams(Map<String, Object> params,Map orders, Integer start, Integer limit) {
        List<PictureCompressHistory> result = Lists.newArrayList();
        if (limit == null) {
            limit = DEFAULT_NUM;
        }
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            BasicDBObject order = getOrderParams(orders);
            Integer skip = start == null ? 0 : start;
            DBCursor cur = this.dbCollection.find(query).sort(order).skip(skip).limit(limit);//按照视频类型和创建时间倒序
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                PictureCompressHistory pictureCompressHistory = PictureCompressHistoryConvert.castDBObjectToPictureCompressHistory(dbObject);
                result.add(pictureCompressHistory);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    private BasicDBObject getBasicDBObjectByParams(Map params){
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = new BasicDBObject();// 新建查询基类对象 dbo
        if (params != null && params.size() > 0) {
            if (params.get("userId") != null) {
                query.put("userId", ObjectUtils.toLong(params.get("userId")));
                }
            if (params.get("programId") != null) {
                query.put("programId", ObjectUtils.toLong(params.get("programId")));
            }
        }
        return query;
    }


    public Long countPictureCompressHistoryByParams(Map params) {
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = getBasicDBObjectByParams(params);
        return this.dbCollection.count(query);
    }


    private BasicDBObject getOrderParams(Map<String, Object> orders) {
        BasicDBObject order = new BasicDBObject();
        if(null != orders &&orders.size()>0){
            for(String key : orders.keySet()){
                order.append(key, Integer.valueOf(String.valueOf(orders.get(key))));
            }
        }else{
            //默认按照创建时间排序
            order.append("createTime", -1);
        }
        return order;
    }
}
